from application import application

class application_window_fake :
    def __init__ ( self ) :
        self . _title = ''
        self . _call_history = [ ]

class key_up_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_up_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_up_event_fake . __del__ ]
        key_up_event_fake . _deleted_count += 1

class key_down_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_down_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_down_event_fake . __del__ ]
        key_down_event_fake . _deleted_count += 1

class key_return_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_return_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_return_event_fake . __del__ ]
        key_return_event_fake . _deleted_count += 1

class key_backspace_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_backspace_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_backspace_event_fake . __del__ ]
        key_backspace_event_fake . _deleted_count += 1

class key_space_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_space_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_space_event_fake . __del__ ]
        key_space_event_fake . _deleted_count += 1

class key_escape_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_escape_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_escape_event_fake . __del__ ]
        key_escape_event_fake . _deleted_count += 1

class key_ctrl_f5_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_ctrl_f5_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_ctrl_f5_event_fake . __del__ ]
        key_ctrl_f5_event_fake . _deleted_count += 1

class key_f5_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ key_f5_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ key_f5_event_fake . __del__ ]
        key_f5_event_fake . _deleted_count += 1

class mouse_button_1_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ mouse_button_1_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ mouse_button_1_event_fake . __del__ ]
        mouse_button_1_event_fake . _deleted_count += 1

class any_key_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler_func ) :
        widget . _call_history += [ any_key_event_fake . __init__ ]
        self . _widget = widget
        self . _handler_func = handler_func
    def __del__ ( self ) :
        self . _widget . _call_history += [ any_key_event_fake . __del__ ]
        any_key_event_fake . _deleted_count += 1

class mediator_fake :
    def __init__ ( self ) :
        self . _application = None
        self . _application_windows = [ ]
        self . _key_up_events = [ ]
        self . _key_down_events = [ ]
        self . _key_return_events = [ ]
        self . _key_backspace_events = [ ]
        self . _key_space_events = [ ]
        self . _key_escape_events = [ ]
        self . _key_ctrl_f5_events = [ ]
        self . _key_f5_events = [ ]
        self . _mouse_button_1_events = [ ]
        self . _any_key_events = [ ]
        self . _application_title = ''
        self . _on_key_up = False
        self . _on_key_down = False
        self . _on_key_return = False
        self . _on_key_backspace = False
        self . _on_key_space = False
        self . _on_key_escape = False
        self . _on_key_ctrl_f5 = False
        self . _on_key_f5 = False
        self . _on_mouse_button_1 = False
        self . _on_any_key = False
        self . _application_window_reply = None
        self . _key_escape_counter_reply = None
    def application_window_reply ( self , window ) :
        self . _application_window_reply = window
    def key_escape_counter_reply ( self , counter ) :
        self . _key_escape_counter_reply = counter
    def gui_create_application_window_request ( self ) :
        window = application_window_fake ( )
        self . _application_windows += [ window ]
        self . _application . gui_create_application_window_reply ( window )
    def on_key_up ( self ) :
        self . _on_key_up = True
    def on_key_down ( self ) :
        self . _on_key_down = True
    def on_key_return ( self ) :
        self . _on_key_return = True
    def on_key_backspace ( self ) :
        self . _on_key_backspace = True
    def on_key_space ( self ) :
        self . _on_key_space = True
    def on_key_escape ( self ) :
        self . _on_key_escape = True
    def on_key_ctrl_f5 ( self ) :
        self . _on_key_ctrl_f5 = True
    def on_key_f5 ( self ) :
        self . _on_key_f5 = True
    def on_mouse_button_1 ( self ) :
        self . _on_mouse_button_1 = True
    def on_any_key ( self ) :
        self . _on_any_key = True
    def gui_set_application_window_title ( self , window , title ) :
        window . _title = title
    def gui_quit_application ( self , window ) :
        window . _call_history += [ mediator_fake . gui_quit_application ]
    def gui_key_up_event_request ( self , widget , handler_func ) :
        key_up_event = key_up_event_fake ( widget , handler_func )
        self . _key_up_events += [ key_up_event ]
        self . _application . gui_key_up_event_reply ( key_up_event )
    def gui_key_down_event_request ( self , widget , handler_func ) :
        key_down_event = key_down_event_fake ( widget , handler_func )
        self . _key_down_events += [ key_down_event ]
        self . _application . gui_key_down_event_reply ( key_down_event )
    def gui_key_return_event_request ( self , widget , handler_func ) :
        key_return_event = key_return_event_fake ( widget , handler_func )
        self . _key_return_events += [ key_return_event ]
        self . _application . gui_key_return_event_reply ( key_return_event )
    def gui_key_backspace_event_request ( self , widget , handler_func ) :
        key_backspace_event = key_backspace_event_fake ( widget , handler_func )
        self . _key_backspace_events += [ key_backspace_event ]
        self . _application . gui_key_backspace_event_reply ( key_backspace_event )
    def gui_key_space_event_request ( self , widget , handler_func ) :
        key_space_event = key_space_event_fake ( widget , handler_func )
        self . _key_space_events += [ key_space_event ]
        self . _application . gui_key_space_event_reply ( key_space_event )
    def gui_key_escape_event_request ( self , widget , handler_func ) :
        key_escape_event = key_escape_event_fake ( widget , handler_func )
        self . _key_escape_events += [ key_escape_event ]
        self . _application . gui_key_escape_event_reply ( key_escape_event )
    def gui_key_ctrl_f5_event_request ( self , widget , handler_func ) :
        key_ctrl_f5_event = key_ctrl_f5_event_fake ( widget , handler_func )
        self . _key_ctrl_f5_events += [ key_ctrl_f5_event ]
        self . _application . gui_key_ctrl_f5_event_reply ( key_ctrl_f5_event )
    def gui_key_f5_event_request ( self , widget , handler_func ) :
        key_f5_event = key_f5_event_fake ( widget , handler_func )
        self . _key_f5_events += [ key_f5_event ]
        self . _application . gui_key_f5_event_reply ( key_f5_event )
    def gui_global_mouse_button_1_event_request ( self , widget , handler_func ) :
        mouse_button_1_event = mouse_button_1_event_fake ( widget , handler_func )
        self . _mouse_button_1_events += [ mouse_button_1_event ]
        self . _application . gui_global_mouse_button_1_event_reply ( mouse_button_1_event )
    def gui_any_key_event_request ( self , widget , handler_func ) :
        any_key_event = any_key_event_fake ( widget , handler_func )
        self . _any_key_events += [ any_key_event ]
        self . _application . gui_any_key_event_reply ( any_key_event )
    def enter_main_loop ( self , window ) :
        window . _call_history += [ mediator_fake . enter_main_loop ]
    def localization_application_title ( self ) :
        return self . _application_title

class application_tests :
    'application'
    def __init__ ( self , test ) :
        key_up_event_fake . _deleted_count = 0
        key_down_event_fake . _deleted_count = 0
        key_return_event_fake . _deleted_count = 0
        key_backspace_event_fake . _deleted_count = 0
        key_space_event_fake . _deleted_count = 0
        key_escape_event_fake . _deleted_count = 0
        key_ctrl_f5_event_fake . _deleted_count = 0
        key_f5_event_fake . _deleted_count = 0
        mouse_button_1_event_fake . _deleted_count = 0
        any_key_event_fake . _deleted_count = 0
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _application = application ( self . _mediator )
        self . _mediator . _application = self . _application
    def test_create_application_window ( self ) :
        self . _application . start ( )
        self . _test. assume_equal_len ( self . _mediator . _application_windows , 1 )
    def test_window_title ( self ) :
        self . _mediator . _application_title = 'my application title'
        self . _application . start ( )
        self . _test . assume_equal ( self . _mediator . _application_windows [ 0 ] . _title, 'my application title' )
    def test_application_window ( self ) :
        self . _application . start ( )
        self . _application . application_window_request ( )
        self . _test . assume_equal ( self . _mediator . _application_window_reply , self . _mediator . _application_windows [ 0 ] )
    def test_create_application_window ( self ) :
        self . _application . start ( )
        self . _test . assume_equal_len ( self . _mediator . _application_windows , 1 )
    def test_window_title ( self ) :
        self . _mediator . _application_title = 'my application title'
        self . _application . start ( )
        self . _test . assume_equal ( self . _mediator . _application_windows [ 0 ] . _title, 'my application title' )
    def test_ending_run_main_loop ( self ) :
        self . _application . start ( )
        self . _application . run_main_loop ( )
        app_window = self . _mediator . _application_windows [ 0 ]
        self . _test . assume_equal ( app_window . _call_history [ - 1 ] , mediator_fake . enter_main_loop )
    def test_ending_quit ( self ) :
        self . _application . start ( )
        self . _application . run_main_loop ( )
        self . _mediator . _key_up_events = [ ]
        self . _mediator . _key_down_events = [ ]
        self . _mediator . _key_return_events = [ ]
        self . _application . quit_application ( )
        app_window = self . _mediator . _application_windows [ 0 ]
        self . _test . assume_equal ( app_window . _call_history [ - 1 ] , mediator_fake . gui_quit_application )
    
    #
    # key escape event counter
    #

    def test_key_escape_counter_after_start ( self ) :
        self . _application . start ( )
        self . _application . run_main_loop ( )
        self . _application . key_escape_counter_request ( )
        self . _test . assume_equal ( self . _mediator . _key_escape_counter_reply , 0 )
    def test_key_escape_counter_after_key_escape ( self ) :
        self . _application . start ( )
        self . _application . run_main_loop ( )
        self . _mediator . _key_escape_events [ 0 ] . _handler_func ( )
        self . _mediator . _key_escape_events [ 0 ] . _handler_func ( )
        self . _application . key_escape_counter_request ( )
        self . _test . assume_equal ( self . _mediator . _key_escape_counter_reply , 2 )

    #
    # create events tests
    #
        
    def test_create_key_up_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_up' )
    def test_create_key_down_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_down' )
    def test_create_key_return_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_return' )
    def test_create_key_backspace_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_backspace' )
    def test_create_key_space_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_space' )
    def test_create_key_escape_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_escape' )
    def test_create_key_ctrl_f5_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_ctrl_f5' )
    def test_create_key_f5_event ( self ) :
        self . _check_create_arbitrary_event ( 'key_f5' )
    def test_create_mouse_button_1_event ( self ) :
        self . _check_create_arbitrary_event ( 'mouse_button_1' )
    def test_create_any_key_event ( self ) :
        self . _check_create_arbitrary_event ( 'any_key' )
        
    #
    # destroy events tests
    #
        
    def test_destroy_key_up_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_up' )
    def test_destroy_key_down_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_down' )
    def test_destroy_key_return_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_return' )
    def test_destroy_key_backspace_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_backspace' )
    def test_destroy_key_space_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_space' )
    def test_destroy_key_escape_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_escape' )
    def test_destroy_key_ctrl_f5_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_ctrl_f5' )
    def test_destroy_key_f5_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'key_f5' )
    def test_destroy_mouse_button_1_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'mouse_button_1' )
    def test_destroy_any_key_event ( self ) :
        self . _check_destroy_arbitrary_event ( 'any_key' )
         
    #
    # event test helpers
    #
    
    def _check_create_arbitrary_event ( self , event_name ) :
        self . _application . start ( )
        self . _application . run_main_loop ( )
        self . _test . assume_equal_len ( getattr ( self . _mediator , '_%s_events' % event_name ) , 1 )
        self . _test . assume_equal \
            ( getattr ( self . _mediator , '_%s_events' % event_name ) [ 0 ] . _widget 
            , self . _mediator . _application_windows [ 0 ]
            )
        getattr ( self . _mediator , '_%s_events' % event_name ) [ 0 ] . _handler_func ( )
        self . _test . assume_true ( getattr ( self . _mediator , '_on_%s' % event_name ) )
        setattr ( self . _mediator , '_%s_events' % event_name , [ ] )
        self . _test . assume_equal \
            ( eval ( '%s_event_fake' % event_name ) . _deleted_count , 0
            )
    def _check_destroy_arbitrary_event ( self , event_name ) :
        self . _application . start ( )
        self . _application . run_main_loop ( )
        setattr ( self . _mediator , '_%s_events' % event_name , [ ] )
        self . _application . quit_application ( )
        self . _test . assume_equal ( eval ( '%s_event_fake' % event_name ) . _deleted_count , 1 )
